/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import type { TaskProxy } from './task-proxy.js';

/**
 * 用于转换和缓存增量任务的中间的代理
 *
 * @since 2022/9/1
 */
class TaskProxyManager {
  private proxyCache!: Map<string, TaskProxy>;

  constructor() {
    this.reset();
  }

  reset() {
    this.proxyCache = new Map<string, TaskProxy>();
  }

  /**
   * 转换单个增量Task
   *
   * @return {TaskProxy}
   * @param taskPath
   * @param taskProxy
   */
  set(taskPath: string, taskProxy: TaskProxy): TaskProxy {
    this.proxyCache.set(taskPath, taskProxy);
    return taskProxy;
  }

  /**
   * 根据Task的Path获取当前缓存的TaskProxy对象
   *
   * @param {string} taskPath
   * @return {TaskProxy | undefined}
   */
  getTaskProxy(taskPath: string): TaskProxy | undefined {
    if (!this.proxyCache.has(taskPath)) {
      throw new Error(`${taskPath} is not an incremental task.`);
    }
    return this.proxyCache.get(taskPath);
  }

  getAllIncrementalTaskProxy(): TaskProxy[] {
    return [...this.proxyCache.values()];
  }
}

export const taskProxyManager = new TaskProxyManager();
